package com.example.login.Service;

import com.example.login.DO.SysUser;
import com.example.login.Mapper.UserMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;

@Service
@Transactional
public class UserServiceImpl implements UserService{
    @Resource
    private UserMapper usermapper;

    @Override
    public SysUser findBy(String userid) {
        return usermapper.findBy(userid);
    }

    @Override
    public int addBy(String userid,long poseid,String username,String password,String usertype,String email,
                     String phonenumber,String sex,String status) {
        Date date = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        java.sql.Timestamp createTime = Timestamp.valueOf(formatter.format(date));
        return usermapper.addBy(userid,poseid,username,password,usertype,email,phonenumber,sex,status,createTime);
    }

    @Override
    public SysUser login(String userid, String password) {
        SysUser sysUser = usermapper.findBy(userid);
        if(sysUser!=null) {
            if(sysUser.getPassword().equals(password)){
                return sysUser;
            }
        }
        return null;
    }

    @Override
    public int updatepassword(String userid, String password) {
        return usermapper.updatepassword(userid,password);
    }
}
